

package com.hazelcast.security.permission;

public class JobPermission extends InstancePermission {

    private static final int SUBMIT = 1;
    private static final int CANCEL = 2;
    private static final int READ = 4;
    private static final int RESTART = 8;
    private static final int EXPORT_SNAPSHOT = 16;
    private static final int ADD_RESOURCES = 32;
    private static final int ALL = SUBMIT | CANCEL | READ | RESTART | EXPORT_SNAPSHOT | ADD_RESOURCES;


    public JobPermission(String... actions) {
        super("<job>", actions);
    }

    @Override
    protected int initMask(String[] actions) {
        int mask = READ;
        for (String action : actions) {
            if (ActionConstants.ACTION_ALL.equals(action)) {
                return ALL;
            }

            if (ActionConstants.ACTION_SUBMIT.equals(action)) {
                mask |= SUBMIT;
            } else
                if (ActionConstants.ACTION_CANCEL.equals(action)) {
                    mask |= CANCEL;
                } else
                    if (ActionConstants.ACTION_READ.equals(action) || ActionConstants.ACTION_LISTEN.equals(action)) {
                        mask |= READ;
                    } else
                        if (ActionConstants.ACTION_RESTART.equals(action)) {
                            mask |= RESTART;
                        } else
                            if (ActionConstants.ACTION_EXPORT_SNAPSHOT.equals(action)) {
                                mask |= EXPORT_SNAPSHOT;
                            } else
                                if (ActionConstants.ACTION_ADD_RESOURCES.equals(action)) {
                                    mask |= ADD_RESOURCES;
                                } else {
                                    throw new IllegalArgumentException("Configured action[" + action + "] is not supported");
                                }
        }
        return mask;
    }
}
